package class03_一些基础的数据结构.test;

import java.util.Stack;

public class Code06_TwoStacksImplementQueue {
    public static class TwoStacksQueue{
        private Stack<Integer> pushStack;
        private Stack<Integer> pollStack;
        public TwoStacksQueue(){
            this.pushStack = new Stack<>();
            this.pollStack = new Stack<>();
        }
        public void pushToPoll(){
            if(pollStack.isEmpty()){
                while(!pushStack.isEmpty()){
                    pollStack.push(pushStack.pop());
                }
            }
        }
        public void push(Integer val){
            this.pushStack.push(val);
            pushToPoll();
        }
        public Integer poll(){
            pushToPoll();
            return this.pollStack.pop();
        }
        public Integer peek(){
            pushToPoll();
            return this.pollStack.peek();
        }
    }

    public static void main(String[] args) {
        TwoStacksQueue test = new TwoStacksQueue();
        test.push(1);
        test.push(2);
        test.push(3);
        System.out.println(test.peek());
        System.out.println(test.poll());
        System.out.println(test.peek());
        System.out.println(test.poll());
        System.out.println(test.peek());
        System.out.println(test.poll());
    }
}
